package com.ideal.gpt.ability.docqa;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.ideal.gpt.ability.docqa.TF_Splitter.TfInfo;
import com.ideal.gpt.ability.docqa.splitter.PairPrefix;
import com.ideal.gpt.ability.docqa.splitter.ResponseSplitter;
import com.lzwork.utility.collection.CollUtility;
import com.lzwork.utility.collection.MapSorter;
import com.lzwork.utility.collection.MapSorter.SortType;
import com.lzwork.utility.pair.StringPair;
import com.lzwork.utility.str.StrUtility;

/**
 * @author LiZheng
 * @date 2023年9月11日 下午5:12:36
 */

public class QaFiller
{
	public static int maxFill = 3;
	
	public static List<StringPair> makeQuestions(Collection<TfInfo> infos)
	{
		List<StringPair> result = new ArrayList<>();

		for (TfInfo info : infos)
		{
			StringPair sp = makeQuestion(info);
			result.add(sp);
		}

		return result;
	}

	public static StringPair makeQuestion(TfInfo info)
	{
		return makeQuestion(info.getSent(), info.getKeywords());
	}

	public static StringPair makeQuestion(String question, List<String> keywords)
	{
		List<String> picked = CollUtility.randomGetItems(keywords, maxFill);
		
		String filler = " [_______] ";
		int fillerLen = filler.length();

		String q = question;

		Map<String, Integer> indexMap = new HashMap<>();

		for (Object o : picked)
		{
			String a = (String) o;
			List<Integer> allIdx = StrUtility.indexOfAll(q, a);

			Integer index = CollUtility.randomGetItem(allIdx);
			
			if (index == null)
			{
				index = 0;
			}

			int shift = fillerLen - a.length();
			for (Entry<String, Integer> e : indexMap.entrySet())
			{
				int i = e.getValue();
				if (i > index)
					e.setValue(i + shift);
			}
			indexMap.put(a, index);

			String first = q.substring(0, index);
			String second = q.substring(index + a.length());

			q = first + filler + second;
		}

		List<String> aList = MapSorter.keySortByValue(indexMap, SortType.INCREASING);

		return new StringPair(q, StrUtility.combineString(aList, ", "));
	}

	public static String resultToString(List<StringPair> result, boolean noAnswer)
	{
		List<String> output = new ArrayList<>();

		int index = 1;
		for (StringPair sp : result)
		{
			String head = result.size() > 1 ? index + ". " : "";
			head += "问题：";

			output.add(head + sp.getKey());
			if (!noAnswer)
				output.add("答案： " + sp.getValue() + "\n");
			index++;
		}

		return StrUtility.combineString(output, "\n");
	}

	public static void main(String[] args)
	{
		String content = "广告经营许可证管理办法(详情请见附件)";

		PairPrefix prefix = new PairPrefix("核心句子", "核心词", false, false);
		List<StringPair> qasp = prefix.splitByParagraph(content);
		if (!qasp.isEmpty())
		{
			ResponseSplitter splitter = new ResponseSplitter(1, true);
			List<StringPair> sps = splitter.makeFromKeywords(qasp);

			System.out.println("*************");
			System.out.println(content);
			System.out.println("-------------");
			System.out.println(StrUtility.combineString(qasp, "\n"));
			System.out.println("-------------");
			System.out.println(StrUtility.combineString(sps, "\n"));
			System.out.println("*************");

			List<String> output = new ArrayList<>();
			int index = 1;
			for (StringPair sp : sps)
			{
				output.add(index + ". " + sp.getKey());
				output.add("\n答案： " + sp.getValue() + "\n\n");
				index++;
			}

			System.out.println(StrUtility.combineString(output, "\n"));
		}
	}
}
